﻿namespace LeadMaster.Controllers
{
    using System;
  using System.Data.Entity;
    using System.Linq;
  using System.Web.Mvc;
    using LeadMaster.Models;
    using LeadMaster.DAL;
    using LeadMaster.ViewModels;
    using System.Web.Security;

    public class DiaryController : Controller
    {
        private DunsmoreContext db = new DunsmoreContext();

        public ActionResult Index()
        {
            MembershipUser currentUser = Membership.GetUser(User.Identity.Name, userIsOnline: true);

            return PartialView(db.Diary.Where(m => m.UserID == (Guid)currentUser.ProviderUserKey && m.Completed==null).OrderBy(m => m.Scheduled).ToList());
        }

        public ActionResult Details(int id = 0)
        {
            Diary diary = db.Diary.Find(id);
            if (diary == null)
            {
                return HttpNotFound();
            }
            return View(diary);
        }

        public ActionResult Create(int id, int contactid = 0, int leadid = 0)
        {
            ViewModelDiary viewModel = new ViewModelDiary();
            if (id==0)
            {
                // new diary entry with no Lead selected                
            }
            else
            {
                // user is on a particular Lead
                viewModel.Lead = db.Leads.Find(id);
                viewModel.Company = db.Companies.Find(viewModel.Lead.CompanyID);
                viewModel.Diary = new Diary();
                viewModel.Diary.CompanyID = id;
                viewModel.Diary.ContactID = contactid;
                viewModel.Diary.LeadID = id;
                viewModel.ContactsList = db.Contacts
                                        .ToList()
                                        .Where(m => m.CompanyID == viewModel.Company.CompanyID)
                                        .OrderBy(m => m.Surname)
                                        .ThenBy(m => m.Forename)
                                        .Select(c => new SelectListItem
                                        {
                                            Value = c.ContactID.ToString(),
                                            Text = string.Format("{0} {1}", c.Forename, c.Surname)
                                        });
            }

            viewModel.DiaryHoldingDate = DateTime.Now.ToString("dd/MM/yyyy");
            viewModel.DiaryHoldingTime = DateTime.Now.ToString("HH:mm");
            viewModel.Diary.UserID = (Guid)Membership.GetUser().ProviderUserKey;

            return PartialView(viewModel);
        }


        [HttpPost]
        public ActionResult Create(ViewModelDiary viewModel)
        {
            if (ModelState.IsValid)
            {
                string intDateTime = viewModel.DiaryHoldingDate + " " + viewModel.DiaryHoldingTime;
                viewModel.Diary.Scheduled = Convert.ToDateTime(intDateTime);
                db.Diary.Add(viewModel.Diary);
                db.SaveChanges();
            }
            return RedirectToAction("Index");
        }

        public ActionResult Edit(int id = 0)
        {
            Diary diary = db.Diary.Find(id);
            if (diary == null)
            {
                return HttpNotFound();
            }
            return View(diary);
        }

        [HttpPost]
        public ActionResult Edit(Diary diary)
        {
            if (ModelState.IsValid)
            {
                db.Entry(diary).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(diary);
        }

        public ActionResult Delete(int id = 0)
        {
            Diary diary = db.Diary.Find(id);
            if (diary == null)
            {
                return HttpNotFound();
            }
            return View(diary);
        }

        public ActionResult CompleteSchedule(int id = 0)
        {
            Diary diary = db.Diary.Find(id);
            diary.Completed = true;
            db.Entry(diary).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Diary diary = db.Diary.Find(id);
            db.Diary.Remove(diary);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}